package com.example.finalwork;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.finalwork.javabean.Note;

import java.util.List;

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
    private List<Note> mBeanList;
    private LayoutInflater layoutInflater;
    private Context mContext;
    private NoteDbOpenHelper noteDbOpenHelper;

    public NoteAdapter(Context context,List<Note> mBeanList){
        this.mBeanList = mBeanList;
        this.mContext = context;
        layoutInflater = LayoutInflater.from(mContext);
        noteDbOpenHelper = new NoteDbOpenHelper(mContext);

    }



    @NonNull
    @Override
    public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = layoutInflater.inflate(R.layout.list_item, parent, false);

        NoteViewHolder noteViewHolder = new NoteViewHolder(view);
        return noteViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull NoteViewHolder holder, int position) {
        Note note = mBeanList.get(position);

        holder.mTvTitle.setText(note.getTitle());
        holder.mTvContent.setText(note.getContent());

        holder.rlContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,EditActivity.class);
                intent.putExtra("note",note);
                mContext.startActivity(intent);
            }
        });

        holder.rlContainer.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Dialog dialog = new Dialog(mContext, android.R.style.ThemeOverlay_Material_Dialog_Alert);
                View view = layoutInflater.inflate(R.layout.delete, null);
                TextView delete = view.findViewById(R.id.delete);
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int row = noteDbOpenHelper.deleteFromDbById(note.getId());
                        if (row > 0){
                            deleteData(position);
                            dialog.dismiss();
                        }
                    }
                });

                dialog.setContentView(view);

                dialog.show();
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        return mBeanList.size();
    }

    public void refreshData(List<Note> notes){
        this.mBeanList = notes;
        notifyDataSetChanged();
    }

    public void deleteData(int pos){
        mBeanList.remove(pos);
        notifyItemRemoved(pos);
    }

    class NoteViewHolder extends RecyclerView.ViewHolder{
        TextView mTvTitle;
        TextView mTvContent;
        TextView mTvTime;
        ViewGroup rlContainer;

        public NoteViewHolder(@NonNull View itemView) {
            super(itemView);
            this.mTvTitle = itemView.findViewById(R.id.tvtitle);
            this.mTvContent = itemView.findViewById(R.id.tvcontent);
            this.mTvTime = itemView.findViewById(R.id.tvtime);
            this.rlContainer = itemView.findViewById(R.id.rl_item_container);
        }
    }
}
